package top.rzhsky.controller;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import top.rzhsky.bean.Message;
import top.rzhsky.bean.Type;
import top.rzhsky.bean.User;
import top.rzhsky.main.Login;
import top.rzhsky.util.AlertUtil;

import java.io.IOException;

import java.util.logging.Logger;


public class LoginController {
    @FXML
    private TextField account;
    @FXML
    private PasswordField password;

    public static ImController imController;

    private Scene scene;

    private static LoginController instance;

    public LoginController() {
        instance = this;
    }

    public static LoginController getInstance() {
        return instance;
    }

    private Logger logger = Logger.getLogger("LoginController");

    @FXML
    public void registerAction() throws IOException {

        Stage primaryStage = new Stage();
        Parent root = FXMLLoader.load(getClass().getResource("/top/rzhsky/fxml/register.fxml"));
        primaryStage.setTitle("注册");
        primaryStage.setScene(new Scene(root));
        primaryStage.getIcons().add(new Image(Login.class.getResourceAsStream("/img/haoyou.png")));
        primaryStage.show();

    }

    public void loginAction() {
        try {
            Message message = new Message();

            String accountText = account.getText();
            String passwordText = password.getText();
            User user = new User();
            user.setAccount(accountText);
            user.setPassword(passwordText);
            message.setFrom(user);
            message.setType(Type.LOGIN);

            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/top/rzhsky/fxml/im.fxml"));
            Parent window = (BorderPane) fxmlLoader.load();
            imController = fxmlLoader.getController();
            Listener listener = new Listener(message, imController);
            new Thread(listener).start();

            this.scene = new Scene(window);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void intoMain(Message message) {
        Platform.runLater(() -> {
            Stage primaryStage = (Stage) account.getScene().getWindow();
            primaryStage.setResizable(true);
            primaryStage.setOnCloseRequest((WindowEvent e) -> {
                Platform.exit();
                System.exit(0);
            });

            primaryStage.setScene(this.scene);
            primaryStage.setTitle("主界面");

            primaryStage.getIcons().add(new Image(Login.class.getResourceAsStream("/img/haoyou.png")));

            primaryStage.setResizable(false);

            imController.setAccount(message.getFrom().getAccount());
            imController.setBirth(message.getFrom().getBirthday().toString());
            imController.setNickName(message.getFrom().getNickName());
            imController.setSex(message.getFrom().getSex());
            imController.setFromUser(message.getFrom());
        });
    }

    public void failLogin() {
        logger.info("failLogin() method Enter");
        Platform.runLater(() -> AlertUtil.alert("登陆失败", "账号或密码不正确", Alert.AlertType.CONFIRMATION, ButtonType.OK));
        logger.info("failLogin() method Exit");

    }

    public void hasConnected(Message receiveMessage) {
        Platform.runLater(() -> AlertUtil.alert("登陆失败", "该账号已登陆", Alert.AlertType.CONFIRMATION, ButtonType.OK));
    }

    public void loginByEnter(KeyEvent keyEvent) {
        if (keyEvent.getCode() == KeyCode.ENTER) {
            loginAction();
        }
    }
}
